package com.cskaoyan.servlet;

import com.cskaoyan.bean.vo.GoodsVo;
import com.cskaoyan.service.AdminService;
import com.cskaoyan.service.AdminServiceImpl;
import com.cskaoyan.service.GoodsService;
import com.cskaoyan.service.GoodsServiceImpl;
import com.cskaoyan.util.JacksonUtil;
import com.cskaoyan.util.ResponseUtil;
import com.cskaoyan.util.URIUtil;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

/**
 * @author stone
 * @date 2023/03/20 09:10
 */
@WebServlet("/admin/goods/*")
public class AdminGoodsServlet extends HttpServlet {

    private GoodsService goodsService = new GoodsServiceImpl();
    //private AdminService adminService = new AdminServiceImpl();
    private AdminService adminService;// = new AdminServiceImpl();
    @Override
    public void init() throws ServletException {
        ServletContext servletContext = getServletContext();
        Map map = (Map) servletContext.getAttribute("applicationData");
        adminService = (AdminService) map.get("adminService");
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String operation = URIUtil.getOperation(this.getClass(),request);
        switch (operation) {
            case "/create":
                create(request, response);
                break;
        }
    }

    private void create(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String jsonStr = request.getReader().readLine();
        GoodsVo goodsVo = JacksonUtil.read(jsonStr, GoodsVo.class);
        ResponseUtil.responseJson(response,goodsService.create(goodsVo));
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
